function SearchService() { }

SearchService.prototype = {

    curPage: -1,
    options: null,
    searchPullShow: false,
    bookmarkPullShow: false,
    searchLoading: false,
    bookmarkLoading: false,
    
    showItemList: function(pitemSearchOption, pageNumber, callback){ //▼サーバ経由
        if (pitemSearchOption != null) {
            SearchService.prototype.options = pitemSearchOption;
        }
        var options = { keyword: SearchService.prototype.options.keyword,
                        page: (pageNumber > 0 ? pageNumber : SearchService.prototype.curPage),
                        minPrice: SearchService.prototype.options.fromPrice, 
                        maxPrice: SearchService.prototype.options.toPrice, 
                        creditCardFlag: SearchService.prototype.options.cardItemSch,
                        postageFlag: SearchService.prototype.options.postageItemSch };                                           
        
        DetailProductService.prototype.getItemInfo({action: "findbykeyword", options: options}, function(data) {
            SearchService.prototype.createItemList(data, callback);
        }, function(err) {
            $('#schItemList').append("<li><h3>検索できませんでした。</h3></li>").listview('refresh');
            Util.prototype.hideSpinner();
        });
    },
    
    createItemList: function(data, callback){
        $(document).data('loading', true);
        var list = "";
        if (data.items.length == 0 || data.pageCount == 0) {
            list = "<li><h3>  該当する商品が見つかりませんでした</h3></li>";
        } else {
            for(var i=0; i<data.items.length; i++){
                var item = data.items[i],
                price = Util.prototype.addComma(item.itemPrice),
                image = (item.imageFlag == true) ? item.smallImageUrl : Messages.prototype.NO_IMAGE,
                priceOptions = '',
                ecsiteName = DetailProductService.prototype.getEcNameByECcode(item.ecSiteCode);
                
                priceOptions = (item.creditCardFlag == 1) ? '<span class="options">カードOK</span>' : '';
                if(item.postageFlag == 0){
                    if (priceOptions !== ''){
                        priceOptions += '&nbsp;&nbsp;&nbsp;';
                    }
                    priceOptions += '<span class="options">送料込み／送料無料</span>';
                }
                list += '<table class="searchTable" itemName="'+item.itemName+'" ecSiteCode="'+item.ecSiteCode+'" itemCode="'+item.itemCode+
                                '" image="'+image+'" genreIdOriginal="'+item.genreIdOriginal+'">'+
                            '<tr><td rowspan="3" class="bm_star"><div class="item_head"><span class="star_checkbox" id="star'+data.page+'-'+i+'">&#9733;</span></div></td>'+
                                '<td colspan="2"><div style="overflow: hidden;" class="shopDetailLink">'+item.itemShortName+'</div></td></tr>'+
                            '<tr class="shopDetailLink"><td rowspan="2" class="itemImage" style="background-image:url('+image+');"></td></tr>'+
                            '<tr class="shopDetailLink"><td class="itemInfor"><div style="overflow: hidden; margin-bottom:10px;><p class="detail">'+ecsiteName+':'+
                                '<span class="shopName">'+item.shopName+'</span></p></div>'+
                                '<p class="detail"><span class="price">'+price+'円</span></p><p>'+ priceOptions + 
                        '</p></td></tr></table>';
            }
            
            SearchService.prototype.curPage = (parseInt(data.page, 10) + 1);
            if (data.pageCount >= SearchService.prototype.curPage){
                SearchService.prototype.searchPullShow = true;
                $('.itemsearch-pull').show();                
            } else {
                SearchService.prototype.searchPullShow = false;
                $('.itemsearch-pull').hide(); 
                SearchService.prototype.curPage = -1;
            }
        }
        $('#schItemList').append(list).listview('refresh');
        
        $(document).data('loading', false);
        
        if (callback != null) {
            callback();
        } else {            
            Util.prototype.hideSpinner();
        }
        
        // check and make star here
        if (data.items.length > 0) {
            setTimeout(function() {
                SearchService.prototype.makeStarBookmarkedItems(data.items, data.page);    
            }, 500);            
        }
    },
    
    makeStarBookmarkedItems: function (items, page) {
        for (var i = 0; i < items.length; i ++) {
            // check in DB
            BookmarksService.prototype.getBookmarkIDByECsiteCodeAndItemId(items[i].ecSiteCode, items[i].itemCode, $('table.searchTable td.bm_star div.item_head span#star'+ page + '-' + i), function(ui, results) {
                if(results.rows.length > 0) {
                    ui.addClass('star_clicked');
                    ui.attr('bmId', results.rows.item(0).id);
                } 
            });
        }
    }
};